package org.qcit.timeplugin;

import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

/**
 * 字节码修改
 */
public class TimeCountClassVisitor extends ClassVisitor {
    String methodOwner;
    boolean isInterface;//判断是否为接口或抽象类
    long threshold;//时间阈值
    public TimeCountClassVisitor(ClassVisitor classVisitor,long threshold) {
        super(Opcodes.ASM7, classVisitor);
        this.threshold= threshold;
    }
    /**
     * @param version 类版本
     * @param access 修饰符
     * @param name 类名
     * @param signature 泛型信息
     * @param superName 父类
     * @param interfaces 实现的接口
     */
    @Override
    public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
        super.visit(version, access, name, signature, superName, interfaces);
        this.methodOwner = name;
        this.isInterface = (access & Opcodes.ACC_INTERFACE) != 0;
    }
    /**
     * 扫描类的方法进行调用
     * @param access 修饰符
     * @param name 方法名字
     * @param descriptor 方法签名
     * @param signature 泛型信息
     * @param exceptions 抛出的异常
     * @return
     */
    @Override
    public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
        MethodVisitor methodVisitor = super.visitMethod(access,name,descriptor,signature,exceptions);
        if (!isInterface && "<init>"!=name &&"<clinit>" != name){
            methodVisitor = new TimeCountAdviceAdapter(api,methodOwner,methodVisitor,access,
                    name,
                    descriptor,
                    threshold);
        }
        return methodVisitor;
    }
}
